var TwitterEDJX = {
    display_list_spinner: document.getElementById('display_twitter_list_wait'),
    display_list_select: document.getElementById('display_twitter'),
    display_list_btn: document.getElementById('display_twitter_btn'),
    assoc_list_spinner: document.getElementById('display_twitter_assoc_wait'),
    assoc_list_select: document.getElementById('assoc_twitter'),
    assoc_list_btn: document.getElementById('del_twitter_btn'),
    add_list_spinner: document.getElementById('display_add_list_wait'),
    add_list_select: document.getElementById('add_twitter_box'),
    add_list_btn: document.getElementById('add_twitter_btn'),
    add_invalid_exist: document.getElementById('display_add_invalid_exist'),
    add_invalid_chars: document.getElementById('display_add_invalid_chars'),
    
    name_active: '',
    
    init: function() {
	try {
	    TwitterEDJX.listAll();
	} catch(e) {
	    setTimeout('TwitterEDJX.init()', 1000);
	}
    },
    
    showWaiter: function() {
	TwitterEDJX.display_list_select.style.visibility = 'hidden';
	TwitterEDJX.display_list_select.style.display = 'none';
	TwitterEDJX.display_list_btn.style.visibility = 'hidden';
	TwitterEDJX.display_list_btn.style.display = 'none';
	TwitterEDJX.assoc_list_select.style.visibility = 'hidden';
	TwitterEDJX.assoc_list_select.style.display = 'none';
	TwitterEDJX.assoc_list_btn.style.visibility = 'hidden';
	TwitterEDJX.assoc_list_btn.style.display = 'none';
	TwitterEDJX.add_list_select.style.visibility = 'hidden';
	TwitterEDJX.add_list_select.style.display = 'none';
	TwitterEDJX.add_list_btn.style.visibility = 'hidden';
	TwitterEDJX.add_list_btn.style.display = 'none';
	TwitterEDJX.display_list_spinner.style.visibility = 'visible';
	TwitterEDJX.display_list_spinner.style.display = 'inline';
	TwitterEDJX.assoc_list_spinner.style.visibility = 'visible';
	TwitterEDJX.assoc_list_spinner.style.display = 'inline';
	TwitterEDJX.add_list_spinner.style.visibility = 'visible';
	TwitterEDJX.add_list_spinner.style.display = 'inline';
    },
    
    showSelect: function() {
	TwitterEDJX.display_list_select.style.visibility = 'visible';
	TwitterEDJX.display_list_select.style.display = 'inline';
	TwitterEDJX.display_list_btn.style.visibility = 'visible';
	TwitterEDJX.display_list_btn.style.display = 'inline';
	TwitterEDJX.assoc_list_select.style.visibility = 'visible';
	TwitterEDJX.assoc_list_select.style.display = 'inline';
	TwitterEDJX.assoc_list_btn.style.visibility = 'visible';
	TwitterEDJX.assoc_list_btn.style.display = 'inline';
	TwitterEDJX.add_list_select.style.visibility = 'visible';
	TwitterEDJX.add_list_select.style.display = 'inline';
	TwitterEDJX.add_list_btn.style.visibility = 'visible';
	TwitterEDJX.add_list_btn.style.display = 'inline';
	TwitterEDJX.display_list_spinner.style.visibility = 'hidden';
	TwitterEDJX.display_list_spinner.style.display = 'none';
	TwitterEDJX.assoc_list_spinner.style.visibility = 'hidden';
	TwitterEDJX.assoc_list_spinner.style.display = 'none';
	TwitterEDJX.add_list_spinner.style.visibility = 'hidden';
	TwitterEDJX.add_list_spinner.style.display = 'none';
    },
    
    listAll: function() {
	TwitterEDJX.showWaiter();
	frame = document.createElement('iframe');
	frame.setAttribute('src', '/edjx/twitter/list/' + tool_name);
	frame.setAttribute('name', 'ajax_callback_x_frame');
	frame.setAttribute('style', 'display: none;');
	document.getElementsByTagName('body')[0].appendChild(frame);
    },
    
    update: function(accounts, hasActive, active_account) {
	TwitterEDJX.display_list_select.options.length = 0;
	TwitterEDJX.assoc_list_select.options.length = 0;
	var displayed = " (displayed)";
	if(hasActive) {
	    TwitterEDJX.display_list_select.options[TwitterEDJX.display_list_select.options.length] = new Option(active_account + displayed, active_account);
	    TwitterEDJX.assoc_list_select.options[TwitterEDJX.assoc_list_select.options.length] = new Option(active_account + displayed, active_account);
	}
	TwitterEDJX.display_list_select.options[TwitterEDJX.display_list_select.options.length] = new Option('[--None--]' + (hasActive == true ? '' : displayed), '~');
	for(x in accounts) {
	    if(accounts[x].toString() == active_account.toString()) {
	    } else {
		TwitterEDJX.display_list_select.options[TwitterEDJX.display_list_select.options.length] = new Option(accounts[x], accounts[x]);
		TwitterEDJX.assoc_list_select.options[TwitterEDJX.assoc_list_select.options.length] = new Option(accounts[x], accounts[x]);
	    }
	}
	TwitterEDJX.name_active = active_account;
	TwitterEDJX.resetEvent();
	TwitterEDJX.showSelect();
    },
    
    resetEvent: function() {
	TwitterEDJX.assoc_list_btn.disabled = true;
	TwitterEDJX.add_list_btn.disabled = true;
	TwitterEDJX.add_list_select.value = '';
    },
    
    updateEventAssoc: function() {
	var selIndx = TwitterEDJX.assoc_list_select.selectedIndex;
	if(selIndx == -1) {
	    TwitterEDJX.assoc_list_btn.disabled = true;
	} else {
	    TwitterEDJX.assoc_list_btn.disabled = false;
	}
    },
    
    updateEventAdd: function() {
	if(TwitterEDJX.add_list_select.value != '') {
	    TwitterEDJX.add_list_btn.disabled = false;
	} else {
	    TwitterEDJX.add_list_btn.disabled = true;
	}
    },
    
    addButtonEvent: function() {
	TwitterEDJX.showWaiter();
	var invalid = TwitterEDJX.invalidAdd_Characters();
	if(invalid == true) {
	    TwitterEDJX.showSelect();
	} else {
	    frame = document.createElement('iframe');
	    frame.setAttribute('src', '/edjx/twitter/add/' + tool_name + '/' + TwitterEDJX.add_list_select.value);
	    frame.setAttribute('name', 'ajax_callback_x_frame');
	    frame.setAttribute('style', 'display: none;');
	    document.getElementsByTagName('body')[0].appendChild(frame);
	}
	
    },
    
    delButtonEvent: function() {
	TwitterEDJX.showWaiter();
	frame = document.createElement('iframe');
	frame.setAttribute('src', '/edjx/twitter/del/' + tool_name + '/' + TwitterEDJX.assoc_list_select.value);
	frame.setAttribute('name', 'ajax_callback_x_frame');
	frame.setAttribute('style', 'display: none;');
	document.getElementsByTagName('body')[0].appendChild(frame);
	function ol_del() {
		setTimeout('TwitterEDJX.listAll()', (1000));
	}
	frame.onload = ol_del();
    },
    
    updButtonEvent: function() {
	TwitterEDJX.showWaiter();
	frame = document.createElement('iframe');
	frame.setAttribute('src', '/edjx/twitter/upd/' + tool_name + '/' + TwitterEDJX.display_list_select.value);
	frame.setAttribute('name', 'ajax_callback_x_frame');
	frame.setAttribute('style', 'display: none;');
	document.getElementsByTagName('body')[0].appendChild(frame);
	function ol_del() {
		setTimeout('TwitterEDJX.listAll()', (1000));
	}
	frame.onload = ol_del();
    },
    
    invalidAdd_Exists: function(boolIn) {
	if(boolIn == true) {
	    TwitterEDJX.add_invalid_exist.style.display = 'inline';
	    TwitterEDJX.add_invalid_exist.style.visibility = 'visible';
	    TwitterEDJX.showSelect();
	} else {
	    TwitterEDJX.add_invalid_exist.style.display = 'none';
	    TwitterEDJX.add_invalid_exist.style.visibility = 'hidden';
	    TwitterEDJX.resetEvent();
	    TwitterEDJX.listAll();
	}
    },
    
    invalidAdd_Characters: function() {
	var regex = new RegExp(/^[a-zA-Z0-9_.]+$/);
	var valid = regex.exec(TwitterEDJX.add_list_select.value);
	if(valid == null) {
	    TwitterEDJX.add_invalid_chars.style.display = 'inline';
	    TwitterEDJX.add_invalid_chars.style.visibility = 'visible';
	    return true;
	} else {
	    TwitterEDJX.add_invalid_chars.style.display = 'none';
	    TwitterEDJX.add_invalid_chars.style.visibility = 'hidden';
	    return false;
	}
    }
    
};

setTimeout('TwitterEDJX.init()', 2000);